package com.nali.dal.statement.query;

import java.io.Serializable;
import java.util.Map;

import com.nali.dal.expression.Expression;
import com.nali.dal.expression.ExpressionValue;
import com.nali.dal.expression.PropertyExpression;

/**
 * ExpressionValueDesc<br>&nbsp;
 * @author sam Created on 2011-5-4
 */
public abstract class ExpressionValueDesc<T extends Expression> {
	public abstract ExpressionValue<T> getExpressionValue(Map<String, Object> params);//接口方法

	public static <E extends Expression> ExpressionValueDesc<E> getInstance(
					String prop, E expression, ColumnUnitConfig columnUnitConfig) {
		return getInstance(new PropertyExpression<E>(prop, expression), columnUnitConfig);
	}
	public static <E extends Expression> ExpressionValueDesc<E> getInstance(
					final PropertyExpression<E> pe, final ColumnUnitConfig columnUnitConfig) {
		if (columnUnitConfig instanceof ConstantColumn) {
			return new ExpressionValueDesc<E>() {
				private ExpressionValue<E> ev = ExpressionValueDesc.getExpressionValue(pe, columnUnitConfig, null);

				@Override
				public ExpressionValue<E> getExpressionValue(Map<String, Object> params) {
					return ev;
				}
			};
		} else {
			return new ExpressionValueDesc<E>() {
				@Override
				public ExpressionValue<E> getExpressionValue(Map<String, Object> params) {
					return ExpressionValueDesc.getExpressionValue(pe, columnUnitConfig, params);
				}

			};
		}
	}

	private static <E extends Expression> ExpressionValue<E> getExpressionValue(
						PropertyExpression<E> pe, ColumnUnitConfig cfg, Map<String, Object> params) {
		return new ExpressionValue<E>(pe, (Serializable) cfg.getColumnUnit().getColumnValue(params));
	}
}
